<?php

namespace App\Modules\Order\Models;

use App\Modules\Payment\IOrder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use phpDocumentor\Reflection\Types\Self_;
use function foo\func;

/**
 * Class CombinedOrder
 * @package App\Modules\Order\Models
 * @property string $title
 * @property string $pay_no
 * @property integer $price
 */
class CombinedOrder extends Model implements IOrder
{
    const STATUS_INIT = "init";
    const STATUS_CLOSE = "close";
    const STATUS_PAID = "paid";

    protected $fillable = [
        'user_id',
        'status',
        'title',
        'pay_no',
        'price',
        'order_id',
        'status',
    ];

    public $casts = [
        'order_id' => 'array',
    ];
    public function getPrice()
    {
        return $this->price;
    }
    public function active(int $money)
    {
        DB::beginTransaction();
        Order::query()
            ->whereIn('id', $this->order_id)
            ->each(function (Order $item) {
                $item->active($item->getPrice());
            });
        $this->status = Self::STATUS_PAID;
        $this->save();
        DB::commit();
    }


    public function offlineActive()
    {
        DB::beginTransaction();
        Order::query()
            ->whereIn('id', $this->order_id)
            ->each(function (Order $item) {
                $item->offline_pay = $item->getPrice();
                $item->active(0);
            });
        $this->status = Self::STATUS_PAID;
        $this->save();
        DB::commit();
    }

    public function getType()
    {
        return self::class;
    }
}
